package com.ygsoft.activiti.listener;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 全部监听
 * 从activiti监听器的使用范围来看，大致分为三种：
 * 1.全局的监听器。
 * 2.连线的监听器。
 * 3.节点的监听器。
 *
 * @author zhx
 * @create 2017-11-22 9:14
 **/
@Service("AllListener")
public class AllListener implements TaskListener ,ExecutionListener {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * ExecutionListener
     * @param delegateExecution
     * @throws Exception
     */
    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        String eventName = delegateExecution.getEventName();
        //start
        if ("start".equals(eventName)) {
            System.out.println("开始start=========");
        }else if ("end".equals(eventName)) {
            System.out.println("结束end=========");
        }else if ("take".equals(eventName)) {
            System.out.println("take=========");
        }
    }

    /**
     * TaskListener
     * @param delegateTask
     */
    @Override
    public void notify(DelegateTask delegateTask) {
        logger.debug("触发了全局监听器, pid={}, tid={}, event={}", new Object[]{
                delegateTask.getProcessInstanceId(), delegateTask.getId(), delegateTask.getEventName()
        });

        String eventName = delegateTask.getEventName();

        if ("create".endsWith(eventName)) {
            System.out.println("create=========");
        }else if ("assignment".endsWith(eventName)) {
            System.out.println("assignment========");
        }else if ("complete".endsWith(eventName)) {
            System.out.println("complete===========");
        }else if ("delete".endsWith(eventName)) {
            System.out.println("delete=============");
        }
    }
}
